/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.Hongbao;

import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import java.io.File;

/**
 * Tools for managing files. Not for public consumption.
 *
 * @hide
 */
public class FileUtils {
    private static final String TAG = "FileUtils";

    /**
     * @return 剩余sd卡容量，单位MB
     */
    public static long getSDFreeSize() {
        //取得SD卡文件路径
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        //获取单个数据块的大小(Byte)
        long blockSize = sf.getBlockSize();
        //空闲的数据块的数量
        long freeBlocks = sf.getAvailableBlocks();
        //返回SD卡空闲大小
        return (freeBlocks * blockSize) / 1024 / 1024; //单位MB
    }


    /**
     * @return 升级包是否存在
     */
    public static boolean isUpdateExists() {
        return isUpdateExists(ConstantsUtil.UPDATE_FILE_NAME);
    }

    public static boolean isUpdateExists(String fileName) {
        if (getSdRoot() != null) {
            File file = new File(getSdRoot(), fileName);
            if (file.exists()) {
                return true;
            }
        }

        return false;
    }


    /**
     * 获取sd卡根目录
     *
     * @return
     */
    public static File getSdRoot() {
        if (isHasSDCard()) {
            return Environment.getExternalStorageDirectory();
        }
        return null;
    }

    /**
     * 是否存在SDCard
     *
     * @return
     */
    public static boolean isHasSDCard() {
        return Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED);
    }

    /**
     * Test if a file lives under the given directory, either as a direct child
     * or a distant grandchild.
     * <p/>
     * Both files <em>must</em> have been resolved using
     * {@link File#getCanonicalFile()} to avoid symlink or path traversal
     * attacks.
     */
    public static boolean contains(File dir, File file) {
        if (file == null)
            return false;

        String dirPath = dir.getAbsolutePath();
        String filePath = file.getAbsolutePath();

        if (dirPath.equals(filePath)) {
            return true;
        }

        if (!dirPath.endsWith("/")) {
            dirPath += "/";
        }
        return filePath.startsWith(dirPath);
    }

    public static boolean deleteContents(File dir) {
        File[] files = dir.listFiles();
        boolean success = true;
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    success &= deleteContents(file);
                }
                if (!file.delete()) {
                    Log.w(TAG, "Failed to delete " + file);
                    success = false;
                }
            }
        }
        return success;
    }

    public static boolean isExists(String filePath) {
        return new File(filePath).exists();
    }
}
